import plotly.offline as pyo
from plotly.graph_objs import *
import chart_studio.plotly as py
import pandas as pd
from pandas import DataFrame
pyo.offline.init_notebook_mode()
lifeExpectancy = pd.read_csv(r"../Data/LifeExpectancyCigarettePrices.csv", index_col = 0)
lifeExpectancy.head()
| Region | Country | Sex | Most sold cigarette brand (US$) | Years | |
|---|---|---|---|---|---|
| 0 | Eastern Mediterranean | Afghanistan | Male | 0.22 | 15 |
| 1 | Eastern Mediterranean | Afghanistan | Female | 0.22 | 16 |
| 2 | Europe | Albania | Male | 1.43 | 18 |
| 3 | Europe | Albania | Female | 1.43 | 20 |
| 4 | Africa | Algeria | Male | 1.14 | 18 |
lifeExpectancy['text'] = lifeExpectancy.apply(lambda x:
"<b>{}</b><br>Life expectancy for {}s at 60: {} years<br>Price of cigarettes: ${:.2f}".format(x['Country'],
x['Sex'],
x['Years'],
float(x['Most sold cigarette brand (US$)'])), axis = 1)
lifeExpectancy.loc[0, 'text']
'<b>Afghanistan</b><br>Life expectancy for Males at 60: 15 years<br>Price of cigarettes: $0.22'
regions = list(lifeExpectancy['Region'].unique())
regions
['Eastern Mediterranean', 'Europe', 'Africa', 'Americas', 'Western Pacific', 'South-East Asia']
sexes = list(lifeExpectancy['Sex'].unique())
sexes
['Male', 'Female']
markerLookup = {'Eastern Mediterranean' : {'symbol' : 'circle'},
'Europe' : {'symbol' : 'square'},
'Africa' : {'symbol' : 'diamond'},
'Americas' : {'symbol' : 'triangle-up'},
'Western Pacific' : {'symbol' : 'cross'},
'South-East Asia' : {'symbol' : 'x'},
'Male' : {'color' : '#663399'},
'Female' :{'color' : '#FF6347'}}
traces = []
for sex in sexes:
for reg in regions:
traces.append({'type' : 'scatter',
'mode' : 'markers',
'x' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),
'Most sold cigarette brand (US$)'],
'y' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex), 'Years'],
'text' : lifeExpectancy.loc [(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),'text'],
'hoverinfo' : 'text',
'marker' : {'color' : markerLookup[sex]['color'],
'symbol' : markerLookup[reg]['symbol'],
'opacity' : 0.7},
'name' : "{}, {}".format(reg, sex)})
layout = {'title' : 'Life Expectancy Against Price of Most Popular Brand of Cigarettes (2011)',
'xaxis' : {'title' : 'Price of most popular brand of cigarettes',
'range' : [0,
lifeExpectancy['Most sold cigarette brand (US$)'].max() * 1.05],
'tickformat' : "${:}"},
'yaxis' : {'title' : 'Life expectancy at age 60 (years)',
'range' : [lifeExpectancy['Years'].min()*0.9,
lifeExpectancy['Years'].max()*1.05],},
'hovermode' : 'closest'}
fig = Figure(data=traces, layout=layout)
pyo.iplot(fig)
traces = []
for sex in sexes:
for reg in regions:
traces.append({'type' : 'scatter',
'mode' : 'markers',
'x' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),
'Most sold cigarette brand (US$)'],
'y' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex), 'Years'],
'text' : lifeExpectancy.loc [(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),'text'],
'legendgroup' : reg,
'hoverinfo' : 'text',
'marker' : {'color' : markerLookup[sex]['color'],
'symbol' : markerLookup[reg]['symbol'],
'opacity' : 0.7},
'name' : "{}, {}".format(reg, sex)})
fig = Figure(data=traces, layout=layout)
pyo.iplot(fig)